package com.admission.web.admin;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Special;
import com.admission.model.SpecialSite;
import com.admission.service.SpecialManager;
import com.admission.service.SpecialSiteManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class SpecialSiteAdminAction extends CrudActionSupport<SpecialSite> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private Page<SpecialSite> page = new Page<SpecialSite>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private SpecialSite entity;
	private SpecialSiteManager specialSiteManager;
	private String ids;
	private String keyword;
	private String spec_id;
	private List<Special> specialList;
	private SpecialManager specialManager;

	public SpecialSite getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(keyword)) {
			PropertyFilter p = new PropertyFilter("LIKES_title", keyword);
			filters.add(p);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		specialSiteManager.getByFilter(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		specialList=specialManager.getAll();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		if(!StringUtils.isEmpty(spec_id))
			entity.setSpecial(specialManager.getSpecialById(spec_id));
		specialSiteManager.save(entity);
		return list();
	}

	@Override
	public String delete() throws Exception {
		specialSiteManager.deleteById(ids);
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id))
			entity = new SpecialSite();
		else
			entity = specialSiteManager.getById(id);

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<SpecialSite> getPage() {
		return page;
	}

	public void setPage(Page<SpecialSite> page) {
		this.page = page;
	}

	public SpecialSite getEntity() {
		return entity;
	}

	public void setEntity(SpecialSite entity) {
		this.entity = entity;
	}

	public SpecialSiteManager getSpecialSiteManager() {
		return specialSiteManager;
	}

	@Autowired
	public void setSpecialSiteManager(SpecialSiteManager specialSiteManager) {
		this.specialSiteManager = specialSiteManager;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getSpec_id() {
		return spec_id;
	}

	public void setSpec_id(String spec_id) {
		this.spec_id = spec_id;
	}

	public List<Special> getSpecialList() {
		return specialList;
	}

	public void setSpecialList(List<Special> specialList) {
		this.specialList = specialList;
	}

	public SpecialManager getSpecialManager() {
		return specialManager;
	}
	@Autowired
	public void setSpecialManager(SpecialManager specialManager) {
		this.specialManager = specialManager;
	}

}
